package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Invoice;
import com.bonuo.crm.domain.Kpi;
import com.bonuo.crm.service.InvoiceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/crm/invoice")
@Api(tags = "开票管理")
public class InvoiceController extends BaseController {
    @Autowired
    private InvoiceService invoiceService;

    //开票列表
    @GetMapping("/list")
    @ApiOperation(value = "开票列表",httpMethod = "GET")
    public TableDataInfo list(Invoice invoice,Integer pageNum,Integer pageSize){
        if (pageNum != null && pageSize != null){
            startPage();
        }
        List<Invoice> invoiceList = invoiceService.selectInvoiceList(invoice);
        //开票总金额
        BigDecimal amount = Optional.ofNullable(invoiceService.selectAmount(invoice.getProjectId()))
                .orElse(BigDecimal.ZERO);
        return getDataTable(invoiceList,amount);
    }

    //添加开票
    @PostMapping("/add")
    @ApiOperation(value = "添加开票",httpMethod = "POST")
    public AjaxResult add(@RequestBody Invoice invoice){
        Integer add = invoiceService.addInvoice(invoice);
        return toAjax(add);
    }

    //修改开票
    @PutMapping("/update")
    @ApiOperation(value = "修改开票",httpMethod = "PUT")
    public AjaxResult update(@RequestBody Invoice invoice){
        Integer update = invoiceService.updateInvoice(invoice);
        return toAjax(update);
    }

    //删除开票
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除开票",httpMethod = "DELETE")
    public AjaxResult delete(@PathVariable Integer id){
        Integer delete = invoiceService.deleteInvoice(id);
        return toAjax(delete);
    }

    //导出开票
    @PostMapping("/export")
    @ApiOperation(value = "导出开票",httpMethod = "POST")
    public void export(HttpServletResponse response , Invoice invoice){
        List<Invoice> invoiceList = invoiceService.selectInvoiceList(invoice);
        ExcelUtil<Invoice> util = new ExcelUtil<Invoice>(Invoice.class);
        util.exportExcel(response, invoiceList, "开票数据");
    }


}
